package cn.edu.gzu.common;


import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class Result<T> extends JSONObject {
    private int code;
    private String message;
    private T data;

    private static <D> Result<D> build(int code, String message, D data){
        Result<D> result = new Result<D>();
        result.put("code",code);
        result.put("message",message);
        result.put("data",data);
        return result;
    }
    //成功
    public static <D> Result<D> success(){
        return build(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),null);
    }
    public static <D> Result<D> success(String message){
        return build(ResultCode.SUCCESS.getCode(),message,null);
    }
    public static <D> Result<D> success(String message,D data){
        return build(ResultCode.SUCCESS.getCode(),message,data);
    }
    public static <D> Result<D> success(ResultCode resultCode){
        return build(resultCode.getCode(),resultCode.getMessage(),null);
    }
    //失败
    public static <D> Result<D> failure(ResultCode resultCode){
        return build(resultCode.getCode(),resultCode.getMessage(),null);
    }
    public static <D> Result<D> failure(ResultCode resultCode,String message){
        return build(resultCode.getCode(),message,null);
    }
    public static <D> Result<D> failure(ResultCode resultCode,D data){
        return build(resultCode.getCode(),resultCode.getMessage(),data);
    }
}
